package com.kamistoat.meimeistore.apisystem.factory;

import com.kamistoat.meimeistore.apisystem.domain.SysFile;
import com.kamistoat.meimeistore.apisystem.feign.RemoteFileService;
import com.kamistoat.meimeistore.commoncore.domain.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件服务降流处理
 */
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService>{

    private static final Logger LOGGER = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);

    /**
     * 在 create覆写方法中重写 RemoteFileService，作为发生 fallback时使用的service
     * 其实就是正常情况下使用 RemoteFileService处理响应。fallback时使用这里重写的 RemoteFileService处理响应
     * @param cause
     * @return
     */
    @Override
    public RemoteFileService create(Throwable cause) {
        LOGGER.error("文件服务调用失败:{}", cause.getMessage());
        return new RemoteFileService() {
            @Override
            public R<SysFile> upload(MultipartFile file) {
                // 直接返回失败
                return R.fail("上传文件失败:" + cause.getMessage());
            }
        };
    }
}